/*
 * Copyright (c) 2014. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package com.dc.gameserver.serverCore.controller.handleController.itemsController;

import com.dc.gameserver.proto.itemserverprotoVo.equLoadingRequest;
import com.dc.gameserver.proto.itemserverprotoVo.equLoadingResponse;
import com.dc.gameserver.proto.itemserverprotoVo.equVO;
import com.dc.gameserver.serverCore.controller.AbstractController.AbstractController;
import com.dc.gameserver.serverCore.controller.AbstractController.IController;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRoleEqu;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.google.protobuf.MessageLite;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:13-12-27 </br>
 *         Time:下午2:34 </br>
 *         Package:{@link com.dc.gameserver.serverCore.controller.handleController.itemsController}</br>
 *         Comment：装备加载请求
 */
@Service
public class equLoadingController extends AbstractController {

    @Resource
    private PlayerService playerService;

    @Override
    public void PostConstruct() {
        int ID= equLoadingRequest.msgID.ID_VALUE;
        GAME_CONTROLLERS[ID]=this;
        IController.MESSAGE_LITE[ID]=equLoadingRequest.getDefaultInstance();
    }

    /**
     * messageLite数据结构体分发
     *
     * @param messageLite 数据载体
     * @param player      active object
     * @throws Exception 注意messageLite应该递交给GC直接处理  ，set null
     */
    @Override
    public void DispatchMessageLit(MessageLite messageLite, PlayerInstance player) throws Exception {
        equLoadingRequest loadingRequest= (equLoadingRequest) messageLite;
        int type=loadingRequest.getType();
        loadingRequest=null;
        FastTable<TRoleEqu> equList=playerService.equLoading(player.getID(),type);
        equLoadingResponse.Builder builder=equLoadingResponse.newBuilder();
        if (equList!=null){
            for (TRoleEqu equ:equList){
                equVO.Builder equvo=equVO.newBuilder();
                equvo.setEquID(equ.getEquid());
                equvo.setEquTypeID(equ.getEqutypeid());
                equvo.setEquLevel(equ.getEqulevel());
                equvo.setEquTimeOut(equ.getEqutimeout());
                equvo.setEquLocation(equ.getEqulocation());
                equvo.setEquPinZhi(equ.getEqupinzhi());
                equvo.setEquPinJie(equ.getEqupinjie());
                equvo.setEquPinJieExp(equ.getEqupinjieexp());
                Double effectnum=equ.getEffectnum1();
                Double effectnumgrowup=equ.getEffectnumgrowup1();
                if (effectnum!=null) equvo.setEffectNum1(effectnum);
                if (effectnumgrowup!=null)equvo.setEffectNumGrowUp1(effectnumgrowup);

                effectnum=equ.getEffectnum2();
                effectnumgrowup=equ.getEffectnumgrowup2();

                if (effectnum!=null)equvo.setEffectNum2(effectnum);
                if (effectnumgrowup!=null)equvo.setEffectNumGrowUp2(effectnumgrowup);

                effectnum=equ.getEffectnum3();
                effectnumgrowup=equ.getEffectnumgrowup3();
                if (effectnum!=null) equvo.setEffectNum3(effectnum);
                if (effectnumgrowup!=null)equvo.setEffectNumGrowUp3(effectnumgrowup);
                equvo.setItemType(equ.getItemtype());
                builder.addEquList(equvo.build());
            }
            equList.clear();
            equList=null;
        }
        builder.setType(type);
        player.DirectSend(wrappedBufferShort(equLoadingResponse.msgID.ID_VALUE,builder.build()));
    }



    public static void main(String[]args){
        System.out.println(System.currentTimeMillis());
        System.out.println(new Date().getTime());
    }
}
